package track4;

import java.util.Random;

public class MyThread extends Thread{
    static double money=100;
    static int count=3;

    static final double MIN_MONEY=0.01;
    @Override
    public void run() {
        synchronized (MyThread.class){
            //判断红包是否抢完
            if (count==0){
                System.out.println(getName()+"没有抢到红包");
            }else {
                double price;
                //判断是否是最后一个红包,把剩余的钱都放进去
                if (count==1){
                    price=money;

                }else {
                    //第一次或第二次拿红包
                    Random random=new Random();
                    //第一个最多拿99.98
                    double bounds =money-(count-1)*MIN_MONEY;
                    price=random.nextDouble(bounds);
                    //有可能随机出来的金额是0
                    if (price<MIN_MONEY){
                        price=MIN_MONEY;
                    }

                }
                //减去金额
                money=money-price;
                //红包个数减1
                count--;
                //打印本次红包信息
                System.out.println(getName()+"抢到了"+price);
            }

        }
    }
}
